package com.cbb.net;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cbb.admin.cbb.R;


/**
 * Created by jxk on 2018/3/05
 * Email:676585325@qq.com
 */

public class MyEmptyViewHelper  {
    public static final int TYPE_NETWORK_ERROR = -1;
    public static final int TYPE_DEFAULT_EMPTY = 0;
    private static int type;
    /**
     * 设置，并返回，空页面View
     *
     * @param view
     * @param type
     * @return
     */
    public static View setEmptyView(View view, View emptyView, int type) {
        final Context context = view.getContext();
        if (emptyView == null || MyEmptyViewHelper.type != type) {
            if (MyEmptyViewHelper.type != type) {
                if (emptyView != null && emptyView.getParent() != null) {
                    ((ViewGroup) emptyView.getParent()).removeView(emptyView);
                }
            }
            MyEmptyViewHelper.type = type;
            emptyView = View.inflate(context, R.layout.layout_empty, null);
            emptyView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup
                    .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            ImageView icon = (ImageView) emptyView.findViewById(R.id.icon);
            TextView text = (TextView) emptyView.findViewById(R.id.text);
         //  Button retry = (Button) emptyView.findViewById(R.id.retry);
            if (emptyView.getParent() != null) {
                ((ViewGroup) emptyView.getParent()).removeView(emptyView);
            }

            switch (type) {
                case TYPE_NETWORK_ERROR:
                    ViewGroup.LayoutParams lp = view.getLayoutParams();
                    icon.setVisibility(View.GONE);
                    text.setText("获得网络数据失败");
                  //  retry.setVisibility(View.VISIBLE);
                   /* retry.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((BaseActivity) context).retry( RETRY_TYPE_NETWORK_ERROR);
                        }
                    });*/
                    ((ViewGroup) view.getParent()).addView(emptyView, lp);
                    break;

                case TYPE_DEFAULT_EMPTY:
                    icon.setImageResource(R.mipmap.icon_default_image);
                    text.setText("暂无数据哦~");
                    //retry.setVisibility(View.GONE);
                    ((ViewGroup) view.getParent()).addView(emptyView);
                    break;
            }
        } else {
            if (emptyView.getParent() != null) {
                ((ViewGroup) emptyView.getParent()).removeView(emptyView);
            }
            ((ViewGroup) view.getParent()).addView(emptyView);
        }

        return emptyView;
    }

    /**
     * 移除空View
     *
     * @param view
     * @param emptyView
     */
    public static void removeEmptyView(View view, View emptyView) {
        ViewGroup group = null;
        if (emptyView != null) {
            group = ((ViewGroup) view.getParent());
            group.removeView(emptyView);
            if (group instanceof ScrollView) {
                group.addView(view, emptyView.getLayoutParams());
            }
        }
        if (group != null && !(group instanceof ScrollView)) {
            view.setVisibility(View.VISIBLE);
        } else if (group == null) {
            view.setVisibility(View.VISIBLE);
        }
    }

}
